package deprecated.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import domain.dao.interfaces.IDAOContactGroup;
import domain.entities.interfaces.IContactGroup;
import util.DomainUtils;
import util.FormArgumentsException;

/**
 * Servlet implementation class ActionGroupDispatcherServlet
 */
public class ActionGroupDispatcherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ActionGroupDispatcherServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String selectedContactGroup = request.getParameter("selected_contactgroup");
		String wantedAction = request.getParameter("wanted_action");
		String newGroupName = DomainUtils.trim(request.getParameter("new_group_name"));

		ApplicationContext context = util.SpringUtils.getApplicationContext();

		try {
			checks(request, selectedContactGroup, wantedAction, newGroupName);

			if(wantedAction.compareTo("show_details_contactgroup") == 0) {
				request.getRequestDispatcher("pages/showContactGroupDetails.jsp").forward(request, response);
			} 
			else if(wantedAction.compareTo("modify_contactgroup") == 0) {
				IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
				List<IContactGroup> contactGroups = (List<IContactGroup>) request.getAttribute("contactGroups");
				int groupId = Integer.parseInt(selectedContactGroup);
				boolean found = false;

				for(IContactGroup cg : contactGroups) {

					if(cg.getGroupId() == groupId) {
						daoContactGroup.updateContactGroup(
								cg,
								Integer.parseInt(selectedContactGroup), 
								newGroupName);


						DomainUtils.displayInfoMessage(
								request, 
								DomainUtils.SHOW_CONTACT_GROUP_ALERT_CURRENT_COLOR, 
								"The contact group name has been successfully updated.");


						request.getRequestDispatcher("pages/showAllContactGroups.jsp").forward(request, response);
						found = true;
						break;
					}
				}

				if(found == false) {
					throw new Exception();
				}

			} 
			else if(wantedAction.compareTo("delete_contactgroup") == 0) {
				IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
				daoContactGroup.deleteContactGroup(
						Integer.parseInt(selectedContactGroup));
				//				DAOFactory.getDaoContactGroup().deleteContactGroup(
				//						null, Integer.parseInt(selectedContactGroup));


				DomainUtils.displayInfoMessage(
						request, 
						DomainUtils.SHOW_CONTACT_GROUP_ALERT_CURRENT_COLOR, 
						"The contact group has been successfully deleted.");


				request.getRequestDispatcher("pages/showAllContactGroups.jsp").forward(request, response);
			} 
			else if(wantedAction.compareTo("add_contact") == 0) {
				request.getRequestDispatcher("pages/addContactToGroup.jsp").forward(request, response);
			} 
			else if(wantedAction.compareTo("remove_contact") == 0) {
				request.getRequestDispatcher("pages/removeFromContactGroup.jsp").forward(request, response);
			} 
		}
		catch(Exception e) {

			if((e instanceof FormArgumentsException) == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "An error occur on the server.");
			}


			DomainUtils.keepParametersContent(request);
			request.getRequestDispatcher("pages/showAllContactGroups.jsp").forward(request, response);
		}
	}

	private void checks(HttpServletRequest request, String selectedContactGroup,
			String wantedAction, String newGroupName) throws Exception {

		HttpSession session = request.getSession(false);

		//Checking session
		if(session == null || session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"You are not currently connected. May be the session expired.");
			throw new FormArgumentsException();
		}

		if(wantedAction == null || wantedAction.isEmpty()){
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "Please choose an action.");
			throw new FormArgumentsException();
		}	

		//no restriction on contact group name except it should not be empty if we want to modify the contactgroup
		if(wantedAction.compareTo("modify_contactgroup") == 0){
			if(newGroupName == null || newGroupName.isEmpty()){
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "The new group name shouldn't be empty.");
				throw new FormArgumentsException();
			}	
		}
	}

}
